package com.xuecheng.content.api;

import com.xuecheng.base.exception.BusinessException;
import com.xuecheng.base.exception.CommonError;
import com.xuecheng.base.model.PageParams;
import com.xuecheng.base.model.PageResult;
import com.xuecheng.content.model.dto.*;
import com.xuecheng.content.model.po.CourseBase;
import com.xuecheng.content.service.CourseBaseService;
import com.xuecheng.content.util.SecurityUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Mr.M
 * @version 1.0
 * @description
 * @date 2023/2/11 15:44
 */
@Api(value = "课程信息管理接口",tags = "课程信息管理接口")
@RestController
public class CourseBaseInfoController {

    @Resource
    private CourseBaseService courseBaseService;

    /**
     * 课程信息分页查询
     * @param pageParams
     * @param queryCourseParamsDto
     * @return
     */
    @ApiOperation("课程查询接口")
    @PreAuthorize("hasAuthority('xc_teachermanager_course_list')")
    @PostMapping("/course/list")
    public PageResult<CourseBase> list(PageParams pageParams, @RequestBody(required=false) QueryCourseParamsDto queryCourseParamsDto) {
        if (pageParams == null || queryCourseParamsDto == null) {
            throw new BusinessException("请求参数为空");
        }
        PageResult<CourseBase> courseBasePageResult = courseBaseService.queryCourseBasePageList(pageParams, queryCourseParamsDto);
        if (courseBasePageResult == null) {
            throw new BusinessException("查询失败");
        }
        return courseBasePageResult;

    }


    /**
     * 添加课程
     * @param addCourseDto
     * @return
     */
    @PostMapping("/course")
    public CourseBaseInfoDto addCourse(@RequestBody @Validated AddCourseDto addCourseDto){
        if (addCourseDto == null) {
            throw new BusinessException("参数为空");
        }
        //获取用户的身份
        SecurityUtil.XcUser user = SecurityUtil.getUser();
        Long companyId = Long.parseLong(user.getCompanyId());

        return courseBaseService.addCourse(companyId, addCourseDto);
    }

    /**
     * 根据id获取课程信息
     * @param id
     * @return
     */
    @GetMapping("/course/{id}")
    public CourseBaseInfoDto getCourseBaseById(@PathVariable("id") Long id){
        if (id == null||id<=0) {
            throw new BusinessException("参数有误");
        }
        return courseBaseService.getCourseBaseInfoById(id);
    }

    /**
     * 更新课程信息
     * @param editCourseDto
     * @return
     */
    @PutMapping("/course")
    public CourseBaseInfoDto updateCourseBaseInfo(@RequestBody @Validated EditCourseDto editCourseDto){
        if (editCourseDto == null) {
            BusinessException.cast(CommonError.REQUEST_NULL);
        }
        return courseBaseService.updateCourseBaseInfo(1232141425L,editCourseDto);
    }


    /**
     * 删除课程信息
     * @param courseId
     */
    @DeleteMapping("/course/{courseId}")
    public void deleteCourseInfo(@PathVariable("courseId")long courseId){
        if (courseId <= 0) {
            BusinessException.cast(CommonError.PARAMS_ERROR);
        }
        courseBaseService.deleteCourseInfo(courseId);
    }

}
